<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
    <div class="nav">
        五子棋对战
    </div>
    <div class="login-container">
        <!-- 登录界面的对话框 -->
        <div class="login-dialog">
            <!-- 提示信息 -->
            <h3>注册</h3>
            <!-- 这个表示一行 -->
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <!-- 这是另一行 -->
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <img id="verificationCodeImg" src="/captcha/getCaptcha" title="看不清？换一张" /><br>
            </div>
            <div class="row">
                <span>请输入验证码</span><input type="text" name="inputCaptcha" id="inputCaptcha"><br>
            </div>

            <!-- 提交按钮 -->
            <div class="row">
                <button id="submit">提交</button>
            </div>
        </div>
    </div> 

    <script src="js/jquery.min.js"></script>
    <script>
        //刷新验证码
        $("#verificationCodeImg").click(function(){
            $(this).hide().attr('src', '/captcha/getCaptcha?dt=' + new Date().getTime()).fadeIn();
        });
        let submitButton = document.querySelector('#submit');
        submitButton.onclick = function() {
            $.ajax({
                url: "/captcha/check",
                type: "post",
                data: {
                    captcha: $("#inputCaptcha").val()
                },
                success:function(result){
                    if(result){
                        let usernameInput = document.querySelector('#username');
                        let passwordInput = document.querySelector('#password');
                        $.ajax({
                            type: 'post',
                            url: '/register',
                            data: {
                                username: usernameInput.value,
                                password: passwordInput.value,
                            },
                            success: function(body) {
                                // 如果注册成功, 就会返回一个新注册好的用户对象.
                                if (body && body.username) {
                                    // 注册成功!
                                    alert("注册成功!")
                                    location.assign('/login.html');
                                } else {
                                    alert("注册失败!用户名可能重复");
                                }
                            },
                            error: function() {
                                alert("注册失败!");
                            }
                        });
                    }else{
                        alert("验证码错误或者过期");
                    }
                }
            });
        }

    </script>
</body>
</html>